package com.guaju.yueshangLife.activity.mine;

import android.databinding.DataBindingUtil;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.adapter.OrderListAdapter;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.bean.MeOrder;
import com.guaju.yueshangLife.bean.PocketLogsBean;
import com.guaju.yueshangLife.databinding.ActivityAcountInfoBinding;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AcountInfoActivity extends BaseActivity {

    ActivityAcountInfoBinding binding;
    private boolean isRefresh = false;
    private int page = 1;
    private int limit = 10;
    private LinearLayoutManager layoutManager;
    private ArrayList<PocketLogsBean.DataBean> orderList = new ArrayList<>();
    private OrderListAdapter orderListAdapter;

    @Override
    protected void loadViewLayout() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_acount_info);
    }

    @Override
    protected void bindViews() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        realLoadData();
    }

    @Override
    protected void setListener() {

        binding.srl.setColorSchemeColors(CommonUtils.getColor(R.color.colorTheme));
        binding.srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                binding.srl.post(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        isRefresh = true;
                        realLoadData();

                    }
                });

            }
        });
    }

    public void scrollRecycleView() {
        binding.recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    lastVisibleItem = layoutManager.findLastVisibleItemPosition();

                    /**StaggeredGridLayoutManager*/
//                    int[] into = new int[(layoutManager).getSpanCount()];
//                    lastVisibleItem = findMax(layoutManager.findLastVisibleItemPositions(into));

                    if (orderListAdapter == null) {
                        return;
                    }
                    if (layoutManager.getItemCount() == 0) {
                        orderListAdapter.updateLoadStatus(orderListAdapter.LOAD_END);
                        return;

                    }
                    if (lastVisibleItem + 1 == layoutManager.getItemCount()
                            && orderListAdapter.getLoadStatus() != orderListAdapter.LOAD_MORE) {
//                        mBookAdapter.updateLoadStatus(BookAdapter.LOAD_PULL_TO);
                        // isLoadMore = true;
                        orderListAdapter.updateLoadStatus(orderListAdapter.LOAD_MORE);
                        page++;
                    }

                    ApiEntity.mePocketLogs(page, limit, new Callback<PocketLogsBean>() {
                        @Override
                        public void onResponse(Call<PocketLogsBean> call, Response<PocketLogsBean> response) {
                            if (response.isSuccessful()) {
                                PocketLogsBean goodsListTotal = response.body();

                                if (goodsListTotal != null) {
                                    List<PocketLogsBean.DataBean> data = goodsListTotal.getData();
                                    //说明刷新到了最底部
                                    if (data == null || data.isEmpty()) {
                                        orderListAdapter.updateLoadStatus(orderListAdapter.LOAD_END);
                                    } else {
                                        for (PocketLogsBean.DataBean bean : data) {
                                            orderList.add(bean);
                                        }
                                        loadCustomData(data);
                                    }
                                }

                            }
                        }

                        @Override
                        public void onFailure(Call<PocketLogsBean> call, Throwable t) {

                        }
                    });
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                /**StaggeredGridLayoutManager*/
//                int[] into = new int[(layoutManager).getSpanCount()];
//                lastVisibleItem = findMax(layoutManager.findLastVisibleItemPositions(into));
            }
        });
    }

    private void loadCustomData(List data) {
        if (orderListAdapter == null) {
            orderListAdapter = new OrderListAdapter(AcountInfoActivity.this, orderList);
            binding.recyclerview.setAdapter(orderListAdapter);
        } else {
            if (data.size() < limit) {
                orderListAdapter.updateLoadStatus(orderListAdapter.LOAD_END);
            } else {
                orderListAdapter.updateLoadStatus(orderListAdapter.LOAD_PULL_TO);

            }
        }
        if (binding.srl.isRefreshing()) {
            binding.srl.setRefreshing(false);
        }
    }


    private void hideEmpty() {
        binding.emptyView.setVisibility(View.GONE);
        binding.llList.setVisibility(View.VISIBLE);
    }

    @Override
    public void showEmpty() {
        binding.emptyView.setVisibility(View.VISIBLE);
        binding.llList.setVisibility(View.GONE);
    }


    private void realLoadData() {
        page = 1;
        ApiEntity.mePocketLogs(page, limit, new Callback<PocketLogsBean>() {
            @Override
            public void onResponse(Call<PocketLogsBean> call, Response<PocketLogsBean> response) {
                if (response.isSuccessful()) {
                    PocketLogsBean body = response.body();
                    if (body == null || body.getData() == null || body.getData().isEmpty()) {
                        showEmpty();
                    } else {
                        hideEmpty();
                        List<PocketLogsBean.DataBean> data = body.getData();
                        orderList.clear();
                        orderList.addAll(data);
                        orderListAdapter.notifyDataSetChanged();
                        if (data.size() < limit) {
                            orderListAdapter.updateLoadStatus(orderListAdapter.LOAD_END);
                        }
                    }
                    if (isRefresh) {
                        isRefresh = false;
                        binding.srl.setRefreshing(false);
                    }

                } else {
                    showEmpty();
                    if (isRefresh) {
                        isRefresh = false;
                        binding.srl.setRefreshing(false);
                    }
                }
            }

            @Override
            public void onFailure(Call<PocketLogsBean> call, Throwable t) {
                showEmpty();
                if (isRefresh) {
                    isRefresh = false;
                    binding.srl.setRefreshing(false);
                }
            }
        });
    }

}
